using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace gears
{
    class Level1And3
    {
        private List<Animation> torches = new List<Animation>();
        private List<Vector2> torchPositions = new List<Vector2>();
        private List<Animation> shaders = new List<Animation>();
        private List<Vector2> shaderPositions = new List<Vector2>();
        private List<Animation> lights = new List<Animation>();
        private List<Vector2> lightPositions = new List<Vector2>();
        private Texture2D mainMirror;
        private Texture2D lightHasp;
        private Vector2 mainMirrorPos;
        private Vector2 lightHaspPos;
        private float mirrorRotation = 0f;
        public int levelState = 1; 
        public int stageState = -1;
        public float statePercent = 1;

        public Level1And3()
        {
            torches.Add(new Animation(ContentLoader.LoadTexture("stage_elements/fire_torch"), 5, 1, 10));
            torches.Add(new Animation(ContentLoader.LoadTexture("stage_elements/fire_torch"), 5, 1, 10));
            torches.Add(new Animation(ContentLoader.LoadTexture("stage_elements/fire_torch"), 5, 1, 10));
            torches.Add(new Animation(ContentLoader.LoadTexture("stage_elements/fire_torch"), 5, 1, 10));
            torches.Add(new Animation(ContentLoader.LoadTexture("stage_elements/fire_torch"), 5, 1, 10));
            torches.Add(new Animation(ContentLoader.LoadTexture("stage_elements/fire_torch"), 5, 1, 10));

            torchPositions.Add(new Vector2(80, 270));
            torchPositions.Add(new Vector2(180, 270));
            torchPositions.Add(new Vector2(280, 270));
            torchPositions.Add(new Vector2(400, 270));
            torchPositions.Add(new Vector2(480, 270));
            torchPositions.Add(new Vector2(640, 270));

            shaders.Add(new Animation(ContentLoader.LoadTexture("lvl1/shader"), 1, 1, 1));
            shaders.Add(new Animation(ContentLoader.LoadTexture("lvl1/shader"), 1, 1, 1));
            shaders.Add(new Animation(ContentLoader.LoadTexture("lvl1/shader"), 1, 1, 1));
            shaders.Add(new Animation(ContentLoader.LoadTexture("lvl1/shader"), 1, 1, 1));
            shaders.Add(new Animation(ContentLoader.LoadTexture("lvl1/shader"), 1, 1, 1));
            shaders.Add(new Animation(ContentLoader.LoadTexture("lvl1/shader"), 1, 1, 1));
            shaderPositions.Add(new Vector2(0, 120));
            shaderPositions.Add(new Vector2(400, 120));
            shaderPositions.Add(new Vector2(0, 240));
            shaderPositions.Add(new Vector2(400, 240));
            shaderPositions.Add(new Vector2(0, 360));
            shaderPositions.Add(new Vector2(400, 360));

            lights.Add(new Animation(ContentLoader.LoadTexture("lvl1/lightHasp1"), 1, 1, 1));
            lights.Add(new Animation(ContentLoader.LoadTexture("lvl1/mirror"), 1, 1, 1));
            lights.Add(new Animation(ContentLoader.LoadTexture("lvl1/lightHasp2"), 1, 1, 1));
            lights.Add(new Animation(ContentLoader.LoadTexture("lvl1/lightHasp3"), 1, 1, 1));
            lights.Add(new Animation(ContentLoader.LoadTexture("lvl1/mirrorLeft"), 1, 1, 1));
            lights.Add(new Animation(ContentLoader.LoadTexture("lvl1/mirrorRight"), 1, 1, 1));
            lights.Add(new Animation(ContentLoader.LoadTexture("lvl1/lens"), 1, 1, 1));

            lightPositions.Add(new Vector2(20, 0));    // always visible lightHasp
            lightPositions.Add(new Vector2(20, 15));   // always visible mirror
            lightPositions.Add(new Vector2(0, 0));     //  big lightHasp
            lightPositions.Add(new Vector2(20, 15));   // always visible small lightHasp
            lightPositions.Add(new Vector2(0, 130));   // lvl1 mirror1  : 5 : 4 /// level1 ///
            lightPositions.Add(new Vector2(750, 135)); // lvl1 mirror2  : 5 : 5
            lightPositions.Add(new Vector2(75, 128));  // lvl1 lens1 : 6 : 6
            lightPositions.Add(new Vector2(360, 130)); // lvl1 lens2 : 6 : 7
            lightPositions.Add(new Vector2(640, 130)); // lvl1 lens3 : 6 : 8 
            lightPositions.Add(new Vector2(750, 268)); // lvl2 mirror1 : 4 : 9 /// level2 ////
            lightPositions.Add(new Vector2(0, 270));   // lvl2 mirror2 : 4 : 10
            lightPositions.Add(new Vector2(650, 260)); // lvl2 lens1 : 6 : 11
            lightPositions.Add(new Vector2(430, 260)); // lvl2 lens2 : 6 : 12
            lightPositions.Add(new Vector2(145, 260)); // lvl2 lens3 : 6 : 13
            lightPositions.Add(new Vector2(0, 370));   // lvl3 mirror1 : 5 : 14 /// level3 ///
            lightPositions.Add(new Vector2(100, 370)); // lvl3 lens3 : 6 : 15
            lightPositions.Add(new Vector2(355, 373)); // lvl3 lens1 : 6 : 16

            mainMirror = ContentLoader.LoadTexture("lvl1/mirror");
            lightHasp = ContentLoader.LoadTexture("lvl1/lightHasp3");
            lightHaspPos = new Vector2(20, 50);
            mainMirrorPos = new Vector2(20, 50);

            foreach (Animation light in lights)
            {
                light.Animating();
            }

            // torch positions:
            // 1: 220, 130
            // 2: 500, 130
        }

        public void Draw(SpriteBatch sprite_batch)
        {
        }

        public void DrawTorches(SpriteBatch sprite_batch)
        {
            int i = 0;


            foreach (Animation _torch in torches)
            {
                _torch.Animating();
                //_torch.DrawAnimation(sprite_batch, torchPositions[i]);
                _torch.DrawScalableAnimation(sprite_batch, torchPositions[i], 0.8f, 1f);
                i++;
            }

        }

        public void DrawShaders(SpriteBatch sprite_batch)
        {
            int i = 0;
            if (levelState == 1)
            {
                foreach (Animation _shader in shaders)
                {
                    _shader.Animating();
                    _shader.DrawScalableAnimation(sprite_batch, shaderPositions[i], 1, 1f);
                    i++;
                }
            }
            else if (levelState == 2)
            {
                i = 0;
                if (stageState == -1)
                {
                    i = 0;
                    foreach (Animation _shader in shaders)
                    {
                        //_shader.Animating();
                        _shader.DrawScalableAnimation(sprite_batch, shaderPositions[i], 1, 1f);
                        i++;
                    }
                    if (mirrorRotation < (float)(Math.PI / 4))
                    {
                        mirrorRotation += (float)(Math.PI / 2) * 0.01f;
                    }
                    else
                    {
                        stageState = 0;
                    }
                }
                else if (stageState < 6)
                {
                    shaders[stageState].Animating();
                    shaders[stageState].DrawScalableAnimation(sprite_batch, shaderPositions[stageState], 1, statePercent);
                    if (stageState < 5)
                        for (int j = stageState + 1; j < 6; j++)
                        {
                            shaders[j].Animating();
                            shaders[j].DrawAnimation(sprite_batch, shaderPositions[j]);
                        }
                    statePercent -= 0.03f;
                    if (statePercent <= 0)
                    {
                        statePercent = 1;
                        stageState++;
                    }
                    DrawLights(sprite_batch, 1);
                }
                else if (stageState == 6)
                {
                    if (mirrorRotation > 0)
                    {
                        mirrorRotation -= (float)(Math.PI / 2) * 0.01f;
                    }
                    else
                    {
                        stageState = 7;
                    }
                    DrawLights(sprite_batch, 1);
                }
                else
                {
                    levelState = 3;
                    //stageState = 1;
                }
                
            }
            else if (levelState == 3)
            {
                i = 0;
                if (statePercent > 0)
                {
                    statePercent -= 0.01f;
                    DrawLights(sprite_batch, statePercent);
                }
                else
                {
                    levelState = 4;
                }
            }

        }

        public void DrawLights(SpriteBatch sprite_batch, float alpha)
        {
            if(stageState != 6 && stageState != 7)
                lights[2].DrawScalableAnimation(sprite_batch, lightPositions[2], 1, alpha);
            //lights[3].DrawScalableAnimation(sprite_batch, lightPositions[1], 1, alpha);
            lights[5].DrawScalableAnimation(sprite_batch, lightPositions[4], 1, alpha);
            lights[5].DrawScalableAnimation(sprite_batch, lightPositions[5], 1, alpha);
            lights[6].DrawScalableAnimation(sprite_batch, lightPositions[6], 1, alpha);
            lights[6].DrawScalableAnimation(sprite_batch, lightPositions[7], 1, alpha);
            lights[6].DrawScalableAnimation(sprite_batch, lightPositions[8], 1, alpha);
            lights[4].DrawScalableAnimation(sprite_batch, lightPositions[9], 1, alpha);
            lights[4].DrawScalableAnimation(sprite_batch, lightPositions[10], 1, alpha);
            lights[6].DrawScalableAnimation(sprite_batch, lightPositions[11], 1, alpha);
            lights[6].DrawScalableAnimation(sprite_batch, lightPositions[12], 1, alpha);
            lights[6].DrawScalableAnimation(sprite_batch, lightPositions[13], 1, alpha);
            lights[5].DrawScalableAnimation(sprite_batch, lightPositions[14], 1, alpha);
            lights[6].DrawScalableAnimation(sprite_batch, lightPositions[15], 1, alpha);
            lights[6].DrawScalableAnimation(sprite_batch, lightPositions[16], 1, alpha);
        }

        public void DrawPrimLights(SpriteBatch sprite_batch)
        {
            lights[0].DrawAnimation(sprite_batch, lightPositions[0]);
            //lights[1].DrawAnimation(sprite_batch,mainMirrorPos);

            sprite_batch.Draw(mainMirror,
                                 new Rectangle((int)mainMirrorPos.X, (int)mainMirrorPos.Y, (int)mainMirror.Width, (int)mainMirror.Height),
                                 null,
                                 Color.White,
                                 mirrorRotation,
                                 new Vector2(mainMirror.Width / 2, mainMirror.Height / 2),
                                 SpriteEffects.None, 0f);
            sprite_batch.Draw(lightHasp,
                                 new Rectangle((int)lightHaspPos.X, (int)lightHaspPos.Y, (int)lightHasp.Width, (int)lightHasp.Height),
                                 null,
                                 Color.White,
                                 mirrorRotation * 2,
                                 new Vector2(0, lightHasp.Height / 2),
                                 SpriteEffects.None, 0f);
            //lights[1].DrawAnimation(sprite_batch, lightPositions[1]);
        }
    }
}
